﻿using System;
using System.Linq;
using System.Threading.Tasks;
using Abp.Domain.Repositories;
using DHJ.Cost.CostAccounting.Dto;
using Microsoft.EntityFrameworkCore;
using DHJ.Cost.CostBooking;
using DHJ.Cost.Workshops;

namespace DHJ.Cost.CostAccounting
{
    public class CostAccountingAppService : CostAppServiceBase, ICostAccountingAppService
    {
        private readonly IRepository<AllocRel, Guid> _costRepository;
        private readonly IRepository<ActualCost, Guid> _actualCostRepository;
        private readonly IRepository<DirectCost, Guid> _directCostRepository;
        private readonly IBatchRatioCalculationContext _batchRatioCalculationContext;

        public CostAccountingAppService(IRepository<AllocRel, Guid> costRepository,
            IRepository<ActualCost, Guid> actualCostRepository,
            IBatchRatioCalculationContext batchRatioCalculationContext,
            IRepository<DirectCost, Guid> directCostRepository)
        {
            _costRepository = costRepository;
            _actualCostRepository = actualCostRepository;
            _batchRatioCalculationContext = batchRatioCalculationContext;
            _directCostRepository = directCostRepository;
        }

        public async Task<NotAccountedCountResult> GetNotAccountedCount(Workshop workshop, int year, int month)
        {
            var actualCount = await _actualCostRepository.GetAll().Where(e => !(e is DirectCost)).Where(e => e.Workshop == workshop &&
                                                                              e.Time.Year == year &&
                                                                              e.Time.Month == month &&
                                                                              !e.IsAccounted).CountAsync();

            var directCount = await _directCostRepository.GetAll().Where(e => e.Workshop == workshop &&
                                                                        e.Time.Year == year &&
                                                                        e.Time.Month == month &&
                                                                        !e.IsAccounted).CountAsync();

            return new NotAccountedCountResult()
            {
                ActualCount = actualCount,
                DirectCount = directCount
            };
        }

        /// <summary>
        /// 均摊车间间接成本
        /// </summary>
        /// <param name="workshop"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public async Task CountCurrentMonthActualCosts(Workshop workshop, int year , int month)
        {
            var costs = await _actualCostRepository.GetAll().Where(e => !(e is DirectCost)).Where(e => e.Workshop == workshop &&
                                                                        e.Time.Year == year &&
                                                                        e.Time.Month == month &&
                                                                        !e.IsAccounted).ToListAsync();

            foreach (var cost in costs)
            {
                cost.Account(_batchRatioCalculationContext);
            }
        }

        /// <summary>
        /// 计算车间直接成本，现在 比例基本为 100%
        /// </summary>
        /// <param name="workshop"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <returns></returns>
        public async Task CountCurrentMonthDirectCosts(Workshop workshop, int year, int month)
        {
            var costs = await _directCostRepository.GetAll().Where(e => e.Workshop == workshop &&
                                                                        e.Time.Year == year &&
                                                                        e.Time.Month == month &&
                                                                        !e.IsAccounted).ToListAsync();

            foreach (var cost in costs)
            {
                cost.Account(_batchRatioCalculationContext);
            }
        }
    }
}